package com.skywolf.chem.pageconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.skywolf.chem.article.entity.LiveInfo;
import com.skywolf.chem.article.mapper.PositionMapper;
import com.skywolf.chem.article.service.ILiveInfoService;
import com.skywolf.chem.pageconfig.entity.ColumnInfo;
import com.skywolf.chem.pageconfig.entity.FirstPage;
import com.skywolf.chem.pageconfig.mapper.FirstPageMapper;
import com.skywolf.chem.pageconfig.pojo.param.AticleParam;
import com.skywolf.chem.pageconfig.pojo.param.ColumnAticles;
import com.skywolf.chem.pageconfig.pojo.param.FirstPagePC;
import com.skywolf.chem.pageconfig.service.IColumnInfoService;
import com.skywolf.chem.pageconfig.service.IFirstPageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * PC首页内容表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-06-22
 */
@Service
public class FirstPageServiceImpl extends ServiceImpl<FirstPageMapper, FirstPage> implements IFirstPageService {


    @Autowired
    FirstPageMapper firstPageMapper;

    @Autowired
    IColumnInfoService columnInfoService;

    @Autowired
    PositionMapper positionMapper;

    @Autowired
    ILiveInfoService liveInfoService;

    @Override
    public FirstPagePC getPCFirstPageConfig() {
        //获取版块一内容
        FirstPagePC configPC = new FirstPagePC();
        List<AticleParam> aticle = firstPageMapper.getPCFirstPageConfig(0);
        configPC.setAticles(aticle);

        aticle = firstPageMapper.getPCFirstPageConfig(1);
        configPC.setAticlesLoops(aticle);

        aticle = firstPageMapper.getPCFirstPageConfig(2);
        configPC.setAticlesVideos(aticle);

        List<ColumnAticles> columnAticleList = new ArrayList<>();
        //查询所有栏目
        QueryWrapper<ColumnInfo> columnInfoQueryWrapper = new QueryWrapper<ColumnInfo>();
        columnInfoQueryWrapper.eq("position_name","first_page");
        columnInfoQueryWrapper.and(wrapper -> wrapper.ne("col_name", "直播").ne("col_name", "赛事").ne("col_name", "赛事预告"));
        columnInfoQueryWrapper.orderByDesc("col_sort");
        List<ColumnInfo> columnList = columnInfoService.list(columnInfoQueryWrapper);
        Integer count = columnList.size()>=3?columnList.size()-2:columnList.size();
        for (int i=0;i<count;i++) {
            ColumnAticles columnAticles = new ColumnAticles();
            ColumnInfo info = columnList.get(i);
            List<AticleParam> colAticle = positionMapper.getColAticle(info.getId(),5);
            columnAticles.setAticles(colAticle);
            columnAticles.setColumnId(info.getId());
            columnAticles.setColumnName(info.getColName());
            columnAticleList.add(columnAticles);
        }
        configPC.setColumnAticles(columnAticleList);
        if(columnList.size()>=3){
            ColumnAticles columnAticles = new ColumnAticles();
            List<ColumnAticles> bottom = new ArrayList<>();
            //获取第一个底部模块
            ColumnInfo info = columnList.get(columnList.size()-1);
            List<AticleParam> colAticle = positionMapper.getColAticle(info.getId(),10);
            columnAticles.setAticles(colAticle);
            columnAticles.setColumnId(info.getId());
            columnAticles.setColumnName(info.getColName());
            bottom.add(columnAticles);

            //获取第二个底部模块
            columnAticles = new ColumnAticles();
            info = columnList.get(columnList.size()-2);
            colAticle = positionMapper.getColAticle(info.getId(),10);
            columnAticles.setAticles(colAticle);
            columnAticles.setColumnId(info.getId());
            columnAticles.setColumnName(info.getColName());
            bottom.add(columnAticles);
            configPC.setBottom1(bottom);
        }

        String sql="limit 5";
        //获取直播
        List<LiveInfo> live = liveInfoService.list(new QueryWrapper<LiveInfo>()
                .eq("video_type",0)
                .orderByDesc("created_time").last(sql));
        configPC.setLive(live);

        //获取回放
        live = liveInfoService.list(new QueryWrapper<LiveInfo>()
                .eq("video_type",1)
                .orderByDesc("created_time").last(sql));
        configPC.setReLive(live);

        return configPC;
    }

    @Override
    public void delPCFirstPageData(Long article_id) {
        firstPageMapper.delPCFirstPageData(article_id);
    }
}
